/* Stereomagery - Some matrix fun on stereo signals can rotate, invert, etc ...
 *
 * Copyright (c) 2008
 * No rights reserved.
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

desc:Stereomagery

slider1:0.5<0,1>Balance (Left/Right)
slider2:0<-90,90,1>Rotation (°)
slider3:100<-200,200,1>Width (%)
slider4:0.5<0,1>Pan (%)

@init
bpos=0;

@slider
pan = slider4;
pan_l = pan <= 0.5 ? 1 : (1-pan)*2;
pan_r = pan >= 0.5 ? 1 : pan*2;

bal = slider1;
bal_l = bal <= 0.5 ? 1 : (1-bal)*2;
bal_r = bal >= 0.5 ? 1 : bal*2;

angle = (slider2   -  45 * (bal-0.5)*2 )/180*3.141592654;
width = slider3 / 100;

cos_coef = cos(angle); sin_coef = sin(angle);

coef_S = width*0.5;
coef_M = 0.5;

tmp1 = (cos_coef-sin_coef);
tmp2 = (sin_coef+cos_coef);

coef_1 = (tmp1*coef_M - tmp2*coef_S) * bal_r * pan_l;
coef_2 = (tmp2*coef_M + tmp1*coef_S) * bal_l * pan_l;
coef_3 = (tmp1*coef_M + tmp2*coef_S) * bal_r * pan_r; 
coef_4 = (tmp2*coef_M - tmp1*coef_S) * bal_l * pan_r;

/*
Some matrix fun ... notes:
The whole processing is done in ONE single matrix operation (which is 4 seperate
normal calculations ...
Anyway we could add volume scaling, mid/side balance, seperate center/mono
volume and stereo volume, and many many more and still only have those 4
calculations.
*/
@sample
l = spl0;
r = spl1;

spl0 = coef_1*r+coef_2*l;
spl1 = coef_3*r+coef_4*l;